<?php


namespace App\Http\Controllers\Admin;


use App\Exceptions\ServiceException;
use App\Exports\ExcelExport;
use App\Http\Controllers\Controller;
use App\Models\Game\GameChannel;
use App\Models\Game\GamePackage;
use App\Models\Game\HotVersion;
use App\Models\Game\Privacy;
use App\Services\Game\PrivacyService;
use App\Utils\MyLog;
use App\Utils\ResponseUtil;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class PrivacyController extends Controller
{
    private PrivacyService $PrivacyService;
    private Request $request;
    public function __construct(Request $request,PrivacyService $PrivacyService)
    {
        $this->PrivacyService = $PrivacyService;
        $this->request = $request;
    }

    public function list(){
        $res = $this->PrivacyService->list($this->request);
        return response()->json($res);
    }
    public function detail($id){
        $detail = Privacy::query()->find($id);
        MyLog::info('detail'.json_encode($detail));
        $pno = [$detail->packages];
        $pnos = implode(',', $pno);
        $pnosArray = explode(',', $pnos);
        $cno = [$detail->channels];
        $cnos = implode(',', $cno);
        $cnosArray = explode(',', $cnos);



        //根据包号查询包号基本信息
        $gamepackage = GamePackage::query()->whereIn('id',$pnosArray)->get();
        $gameChannel = GameChannel::query()->whereIn('id',$cnosArray)->get();
        MyLog::info('$gamepackage'.json_encode($gamepackage));
        $detail['packages']=$gamepackage;
        $detail['channels']=$gameChannel;

        return ResponseUtil::success($detail);
    }

    public function export(){
        $res = $this->PrivacyService->list($this->request);
        $excelRows = [
            ['ID','标题','创建时间']
        ];
        foreach($res['data'] as $datum){
            $excelRows[] = [
                $datum['id'], $datum['title'],$datum['created_at']
            ];
        }
        $excel = new ExcelExport($excelRows);
        return Excel::download($excel, '通知公告.xlsx');
    }

    public function delete(){
        $ids = $this->request->post('ids');
        Privacy::query()->whereIn('id',$ids)->delete();
        return ResponseUtil::success();
    }

    public function add(){
        $post = $this->request->post();
        $data = array_only($post,['title','content','status','channelIds','packageIds','packages','noticeCilentType','name','usercontent','qrocontent','imgurl']);
        $packageIds = data_get($data,'packageIds');
        $channelIds = data_get($data,'channelIds');
        $packageNo = data_get($data,'packages', []);
        $noticeCilentType = (int) data_get($data, 'noticeCilentType'); // 强制转换为整数

        MyLog::info('$data查看全部传入参数'.json_encode($data));
        if ($noticeCilentType === 1) {
            // 检查是否存在 noticeCilentType 等于 1 的数据
            $exists = Privacy::query()->where('noticeCilentType', 1)->exists();

            if ($exists) {
                return response()->json(['code' => 1, 'msg' => '全局数据已存在']);
            }
        }

        $packagenNos = implode(',', array_column($packageNo, 'package_name'));
        $packageIdsString = implode(',', $packageIds);
        $channelIdsString = implode(',', $channelIds);

        $datarow=[];
        $datarow['packages']=$packageIdsString;
        $datarow['name']=data_get($data,'name');

        $datarow['noticeCilentType']=$noticeCilentType;
        $datarow['package_name']=$packagenNos;
        $datarow['channels']=$channelIdsString;
        $datarow['content'] = data_get($data, 'content');
        $datarow['usercontent'] = strip_tags(data_get($data, 'usercontent'));
        $datarow['title'] = data_get($data,'title');
        $datarow['status'] = data_get($data,'status');
        $datarow['qrocontent'] = data_get($data,'qrocontent');

        $datarow['imgurl'] = data_get($data,'imgurl');

        $datarow['created_at'] = now();
        Privacy::query()->insert($datarow);
        return ResponseUtil::success();
    }

    public function edit($id){
        $data = $this->request->post();
        $title = data_get($data,'title');
        $status = data_get($data,'status');
        $content =  data_get($data, 'content');
        $usercontent =  strip_tags(data_get($data, 'usercontent'));
        $channelIds = data_get($data,'channelIds');
        $packageIds = data_get($data,'packageIds');
        $packno = data_get($data,'package_no');
        $name = data_get($data,'name');
        $imgurl= data_get($data,'imgurl');
        $qrocontent = data_get($data,'qrocontent');
        $noticeCilentType = data_get($data,'noticeCilentType');

        $packnoArray = explode(',', $packno);

        $filteredPackageIds = array_diff($packageIds, $packnoArray);

        //获取packagename值
        $packageName = data_get($data,'packages', []);
        //将数组转换成逗号隔开的字符串
        $packagenNames = implode(',', array_column($packageName, 'package_name'));
        $packageIdsString = implode(',', $packageIds);
        $channelIdsString = implode(',', $channelIds);
        MyLog::info('$包的集合'.json_encode($filteredPackageIds));


        $hotvs= Privacy::query()->where('id',$id)->first();
        $pno = [$hotvs->package_no];
        $pnos = implode(',', $pno);
        $pnosArray = explode(',', $pnos);

        Privacy::query()->where('id',$id)->update(['title' => $title,'name' => $name,'qrocontent' => $qrocontent,'imgurl' => $imgurl,"noticeCilentType"=>$noticeCilentType,'status'=>$status,'content'=>$content,'usercontent'=>$usercontent,'package_name'=>$packagenNames,'packages'=>$packageIdsString,'channels'=>$channelIdsString,'updated_at'=>now()]);


        return ResponseUtil::success();
    }




}
